package com.chukun.interview.leetcode;

import java.util.*;

/**
 * @author chukun
 * @version 1.0.0
 * @description 最小栈
 * @createTime 2023年05月07日 20:34:00
 */
public class MinStack {

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());

    }

    private Deque<Integer> commonStack;

    private Deque<Integer> minStack;


    public MinStack() {
        commonStack = new LinkedList<>();
        minStack = new LinkedList<>();
        minStack.push(Integer.MAX_VALUE);
    }

    /**
     * 压栈操作
     * @param val
     */
    public void push(int val) {
        commonStack.push(val);
        Integer peek = minStack.peek();
        int min = Math.min(peek, val);
        minStack.push(min);
    }

    /**
     * 弹栈操作
     */
    public void  pop() {
        commonStack.pop();
        minStack.pop();
    }

    public int  top() {
        return commonStack.peek();
    }

    public int  getMin() {
        return minStack.peek();
    }

}
